package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.PatientBean;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.viewholders.DiscoverPatientViewholder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-07-09.
 * Description:发现页下的患者记录列表
 */
public class DiscoverPatientsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<PatientBean> datas;
    private Context mContext;
    private LayoutInflater mInflater;
    private int patientType = 0;//0未管理 1已管理
    private ImageLoader imageLoader;
    private int screenWidth;

    private patientBindListener listener;

    public DiscoverPatientsAdapter(Context mContext, int type, List<PatientBean> datas, int width,
                                   patientBindListener listener) {
        this.mContext = mContext;
        this.patientType = type;
        this.datas = datas;
        this.listener = listener;
        this.screenWidth = width;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    public void setData(List<PatientBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void cleanData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_discoverpatients, null);
        DiscoverPatientViewholder holder = new DiscoverPatientViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DiscoverPatientViewholder viewholder = (DiscoverPatientViewholder) holder;
        PatientBean bean = datas.get(position);

        LinearLayout.LayoutParams lp_head = new LinearLayout.LayoutParams(screenWidth / 6, screenWidth / 6);
        viewholder.civ_head.setLayoutParams(lp_head);
        DisplayImageOptions option = null;
        long dateBegin = 0;
        try {
             dateBegin = (new Date(Long.valueOf(bean.getAge()))).getTime();
        }catch (NumberFormatException e){
            e.printStackTrace();
            dateBegin =0;
        }
       // long dateBegin = (new Date(Long.valueOf(bean.getAge()))).getTime();
        long dateNow = (new Date()).getTime();
        long age = (dateNow - dateBegin) / 1000 / 60 / 60 / 24 / 365;
        if (bean.getSex().equals("0")) {//male
            if (age > 49) {
                option = ImageOptionUtils.getInstance().getMan50HeadOptions();
            } else {
                option = ImageOptionUtils.getInstance().getMan20HeadOptions();
            }
        } else {//female
            if (age > 49) {
                option = ImageOptionUtils.getInstance().getWoman50HeadOptions();
            } else {
                option = ImageOptionUtils.getInstance().getWoman20HeadOptions();
            }
        }
        imageLoader.displayImage(bean.getImg(), viewholder.civ_head, option);
        viewholder.civ_head.setBorderWidth(1);
        viewholder.civ_head.setBorderColor(ContextCompat.getColor(mContext, R.color.grey_text2));

        viewholder.tv_name.setText(bean.getName());
        if (bean.getSex().equals("0")) {
            viewholder.tv_sex.setText(R.string.male);
        } else if (bean.getSex().equals("1")) {
            viewholder.tv_sex.setText(R.string.female);
        } else {
            viewholder.tv_sex.setText(R.string.unknown);
        }
        if (age < 1) {
            viewholder.tv_age.setText("<1岁");
        } else {
            viewholder.tv_age.setText((int) age + "岁");
        }
        if (bean.getChildbirthStatus().equals("1")) {
            viewholder.tv_child.setVisibility(View.VISIBLE);
        } else {
            viewholder.tv_child.setVisibility(View.GONE);
        }

        if (patientType == 0) {
            viewholder.bt_manage.setVisibility(View.VISIBLE);
            viewholder.ll_nurse.setVisibility(View.GONE);
            viewholder.bt_manage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onBind(position);
                }
            });
        } else {
            viewholder.bt_manage.setVisibility(View.GONE);
            viewholder.ll_nurse.setVisibility(View.VISIBLE);


            viewholder.tv_nurse.setText(bean.getNurseName());
//            viewholder.ll_nurse.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    listener.onBind(position);
//                }
//            });
        }
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface patientBindListener {
        void onBind(int position);
    }
}
